﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net.WebSockets;
using BartonExtension;

namespace WSServer
{
    
    public class WSMessage
    {
        public ArraySegment<byte> content { get; set; }
        public WebSocketMessageType message_type { get; set; }
        public DateTime message_time { get; set; }
        public WSSocket socket { get; set; }
        private object cachedObject { get; set; }
        public T Get<T>()
        {
            try
            {
                if (cachedObject != null)
                    return (T)cachedObject;
                string json = Encoding.UTF8.GetString(content.Array, content.Offset, content.Count);
                cachedObject = json.FromJson<T>();
                return (T)cachedObject;
            }
            catch(Exception)
            {
                return default(T);
            }
        }
    }
}
